package com.fishing.controller;


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.fishing.entity.resource_log_reason;
import com.fishing.util.personal.GoodsNameTransformation;
import com.fishing.util.personal.QLClientServer;

import QL.ButlerGetGlobalInfomationRequest;
import QL.ButlerGetGlobalInfomationResponse;
import QL.ButlerGetGlobalResourceStatRequest;
import QL.ButlerGetGlobalResourceStatResponse;
import QL.ButlerResourceStatItem;
import QL.ButlerResourceStockItem;
import QL.QLClient;
import net.sf.json.JSONArray;


/***
 * 首页
 * @author lrz
 *
 */
@Controller
public class IndexController{
	
	private static QLClient client = new QLClient();
	
	@RequestMapping(value = "index")
	public ModelAndView index( HttpServletRequest request)throws Exception{
    	ModelAndView mv = new ModelAndView("home");
    	return mv;
	}
	
	@RequestMapping(value = "Essential")
	public ModelAndView Essential( HttpServletRequest request)throws Exception{
    	ModelAndView mv = new ModelAndView("Essential");
    	return mv;
	}

    /***
	 * 首页统计
	 * 
	 * 传来的数据为对象的属性值，为了得到自己想要的jsonArray数据顺序
	 * 可以利用jsonObject.put来增加元素,最后利用JSONArray.add(jsonObject)
	 * 
	 * @param statType
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="Essential/list",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	public JSONArray WholeStat(HttpServletRequest request)throws Exception{
		
		//region 获得全局的统计数据
		ButlerGetGlobalInfomationRequest req = new ButlerGetGlobalInfomationRequest();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		ButlerGetGlobalInfomationResponse rsp = new ButlerGetGlobalInfomationResponse();
		QLClientServer.server(req.ZoneId);
		QLClient.Execute(req, rsp);
        JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new  JSONObject(new LinkedHashMap<>());
		
		jsonObject.put("TotalRegisteredCount", rsp.TotalRegisteredCount);
		jsonObject.put("UserOnlineCount", rsp.UserOnlineCount);
		jsonObject.put("TodayRegisteredCount", rsp.TodayRegisteredCount);
		jsonObject.put("BlockUserCount", rsp.BlockUserCount);
		jsonObject.put("CurrencyStock", rsp.CurrencyStock);
		jsonObject.put("BankCurrencyStock", rsp.BankCurrencyStock);
		jsonObject.put("AllCurrencyStock", rsp.CurrencyStock + rsp.BankCurrencyStock);
		jsonObject.put("DiamondStock", rsp.DiamondStock);
		jsonObject.put("BusinessmanCount", rsp.BusinessmanCount);
		jsonObject.put("TotalRecharge", rsp.TotalRecharge/100);
		jsonObject.put("TodayRecharge", rsp.TodayRecharge/100);
		jsonObject.put("TicketStock", rsp.TicketStock);  //奖券
		jsonObject.put("IntegralStock", rsp.IntegralStock/10000); //积分
		jsonObject.put("Warhead1Stock", rsp.Warhead1Stock);
		jsonObject.put("Warhead2Stock", rsp.Warhead2Stock);
		jsonObject.put("Warhead3Stock", rsp.Warhead3Stock);
		jsonObject.put("Warhead4Stock", rsp.Warhead4Stock);
		jsonObject.put("GlobalPrizePool", rsp.GlobalPrizePool);
		jsonObject.put("GlobalGameDifficultyLevel", rsp.GlobalGameDifficultyLevel);
		jsonObject.put("TotalOnlineCurrency", rsp.TotalOnlineCurrency);
		//endregion
		
		//region 全局资源变化对应的变化原因
		ButlerGetGlobalResourceStatRequest req2 = new ButlerGetGlobalResourceStatRequest();
		req2.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		ButlerGetGlobalResourceStatResponse rsp2 = new ButlerGetGlobalResourceStatResponse();
		QLClientServer.server(req.ZoneId);
		QLClient.Execute(req2, rsp2);
		if(rsp2.StockItems != null){
			for(ButlerResourceStockItem stockitem : rsp2.StockItems) {
				Map<String,Object> map = new HashMap<String,Object>();
				long GainByRecharge = 0;  //充值获得
				long FalseGainByRecharge = 0; //非充值获得
				long GainByFishingKill = 0;//捕鱼掉落
				long GainByFishingBonus = 0;//奖金鱼获得
				long CostByFishingShoot = 0;//捕鱼开炮消耗
				long TotalGain = 0;//总获得
				long TotalCost = 0;//总消耗
				long GainByOther = 0;//其他玩法
				for(ButlerResourceStatItem statitem:rsp2.StatItems) {
					if(stockitem.Name.equals(statitem.Name) && statitem.TotalCount != 0) {
						if(statitem.TotalCount > 0) {
							TotalGain += statitem.TotalCount;
						}else {
							TotalCost += -statitem.TotalCount;
						}
						switch((int)statitem.Reason) { //switch不能有long类型
						//非充值获得
						//充值
						case 3:
							GainByRecharge += statitem.TotalCount;
							break;
						case 24:
							GainByRecharge += statitem.TotalCount;
							break;
						case 28:
							GainByRecharge += statitem.TotalCount;
							break;
						case 33:
							GainByRecharge += statitem.TotalCount;
							break;
						case 35:
							GainByRecharge += statitem.TotalCount;
							break;
							
							//捕鱼掉落获得
						case 301:
							GainByFishingKill += statitem.TotalCount;
							break;
							
							//奖金鱼抽奖获得
						case 304:
							GainByFishingBonus += statitem.TotalCount;
							break;
							
							//捕鱼开炮消耗
						case 300:
							CostByFishingShoot += -statitem.TotalCount;
							break;
						}
					}
				}
				//其他玩法获得
				GainByOther = TotalGain - GainByRecharge - GainByFishingKill - GainByFishingBonus;
				FalseGainByRecharge = TotalGain - GainByRecharge;
				jsonObject.put(GoodsNameTransformation.TransformationToEnglish(stockitem.Name) + "FalseGainByRecharge", FalseGainByRecharge);
				jsonObject.put(GoodsNameTransformation.TransformationToEnglish(stockitem.Name) + "TotalGain", TotalGain);
				jsonObject.put(GoodsNameTransformation.TransformationToEnglish(stockitem.Name) + "TotalCost", TotalCost);
				jsonObject.put(GoodsNameTransformation.TransformationToEnglish(stockitem.Name) + "GainByRecharge", GainByRecharge);
				jsonObject.put(GoodsNameTransformation.TransformationToEnglish(stockitem.Name) + "GainByFishingKill", GainByFishingKill);
				jsonObject.put(GoodsNameTransformation.TransformationToEnglish(stockitem.Name) + "GainByFishingBonus", GainByFishingBonus);
				jsonObject.put(GoodsNameTransformation.TransformationToEnglish(stockitem.Name) + "GainByOther", GainByOther);
				jsonObject.put(GoodsNameTransformation.TransformationToEnglish(stockitem.Name) + "CostByFishingShoot", CostByFishingShoot);
				jsonObject.put(GoodsNameTransformation.TransformationToEnglish(stockitem.Name) + "CurrentCount", stockitem.Count);
			}
		}
		//endregion
		
        long AllWaterLine = 0;
		long AllFeeGrab = 0;
		long AllBonus = 0;
		if(rsp2.StockItems != null){
		for(int i = 0;i < rsp2.GamePlatformItems.size();i++) {
			if(rsp2.GamePlatformItems.get(i).GameFlag == 1) {
				//捕鱼3D平台收益
				AllFeeGrab += rsp2.GamePlatformItems.get(i).FeeGrab;
				//捕鱼3D水位 
				AllWaterLine += rsp2.GamePlatformItems.get(i).WaterLine;
			}else if(rsp2.GamePlatformItems.get(i).GameFlag == 3){
				//水浒传平台收益
				AllFeeGrab += rsp2.GamePlatformItems.get(i).FeeGrab;
				//水浒传水位 
				AllWaterLine += rsp2.GamePlatformItems.get(i).WaterLine;
			}else if(rsp2.GamePlatformItems.get(i).GameFlag == 4) {
				//拉霸机平台收益
				AllFeeGrab += rsp2.GamePlatformItems.get(i).FeeGrab;
				//拉霸机水位 
				AllWaterLine += rsp2.GamePlatformItems.get(i).WaterLine;
				//拉霸机彩金池
				AllBonus += rsp2.GamePlatformItems.get(i).Bonus;
			}
		}
		}
		
		jsonObject.put("AllWaterLine", AllWaterLine);
		jsonObject.put("AllFeeGrab", AllFeeGrab);
		jsonObject.put("AllBonus", AllBonus);
		
		jsonArray.add(jsonObject);
		return jsonArray;
	}
    
}
